<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/21
 * Time: 16:56
 */

namespace osc\admin\controller;


use osc\common\controller\AdminBase;
use think\Db;
use think\Request;
use osc\common\service\Excel;

class ContractIncrease extends AdminBase
{
    protected function _initialize(){
        parent::_initialize();
        $this->assign('breadcrumb1','收益回补');
        $this->assign('breadcrumb2','回补列表');
        $this->assign('breadcrumb3','金豆兑换开放名单');

    }

    /**
     * 收益列表
     * @return mixed
     */
    public function index(){
        $param=input('param.');
        $map = null;
        if(!empty($param['username'])){
            $map['username'] = ['like',"%".$param['username']."%"];
        }
        if(!empty($param['dt'])){
            $map["DATE_FORMAT(FROM_UNIXTIME(dt),'%Y-%m-%d')"] = $param['dt'];
        }

        $list = Db::name('contract_increase')
            ->where($map)
			->order('dt desc')
            ->paginate(10,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);

        $page = $list->render();


        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 批量审批
     * @throws \Exception
     */
    public function trial_batch(){
        $data = $this->request->param();
        if(!$data){ $this->redirect('ContractIncrease/index'); }
        $map = [];
        foreach($data as $key=>$value){
            $map[$key]['id'] = $value;
            $map[$key]['state'] = 2;
        }
        $inc = new \osc\admin\model\ContractIncrease();
        $res = $inc->saveAll($map);
        if($res){
            $this->redirect('ContractIncrease/index');
        }else{
            $this->error('审批失败');
        }
    }

    /**
     * 审核打回
     */
    public function repulse(){
        $data = $this->request->param();
        $map = [
            'state'     => 3,
            'desc'      => $data['desc']
        ];
        $res = Db::name('contract_increase')->where('id',$data['id'])->update($map);
        if($res){
            $this->redirect('ContractIncrease/index');
        }else{
            $this->error('打回失败');
        }
    }


    /**
     * 导出收益回补
     * @throws \think\Exception
     */
    public function export_increase()
    {
        $param=input('param.');

        $map = [];
        if(!empty($param['username'])){
            $map['a.username'] = ['like',"%".$param['username']."%"];
        }

        if(!empty($param['info_id'])){
            $map['a.type'] = $param['info_id']-1;
        }

        if(!empty($param['dt'])){
            $map["DATE_FORMAT(FROM_UNIXTIME(a.dt),'%Y-%m-%d')"] = $param['dt'];

        }
//        echo'<pre>';print_r($map);exit;
        $excel=new Excel();

        $field = [
            "a.id"              =>"序号",
            "b.idcardname"      =>"姓名",
            "a.username"        =>"电话号码",
            "a.contract_id"     =>"合约ID",
            "a.type"            =>"合约名称",
            "c.exchange_id"     =>"交易所",
            "c.trade_id"        =>"交易所主账户",
            "a.type_state"      =>"类型",
            "a.inc"             =>"回补额",
            "a.ice"             =>"提货回补额",
            "a.dt"              =>"回补记录时间",
            "b.predeposit"      =>"账号余额"
        ];

        $list = Db::name('contract_increase a,osc_member b,osc_exchange_trade_acc c')
            ->where($map)
            ->where('a.username=b.username and b.username=c.username and c.exchange_id=3')
            ->field($field)
            ->select();
        foreach($list as $key=>&$value){
            if($value['合约名称'] == 0){
                $value['合约名称'] = '自由买卖';
            }else{
                $value['合约名称'] = '目标价认购';
            }

            if($value['类型'] == 0){
                $value['类型'] = '收益回补';
            }else{
                $value['类型'] = '新规则回补';
            }
            $value['回补记录时间'] = date('Y-m-d H:i:s',$value['回补记录时间']);
            $value['交易所']  = '湖南大宗玉石交易平台';
        }
        $ft = "收益回补".date("Y-m-d",time());
//        echo"<pre>";print_r($list);
//        echo"<pre>";print_r($field);exit;
        $excel->setExcelName($ft)
            ->createSheetByList("收益回补",$list,$field)
            ->downloadExcel();
    }


    /**
     * 新增金豆兑换开放名单
     * @return mixed
     */
    public function bonus_new(){
        $param=input('param.');
        $map = null;
        if(!empty($param['username'])){
            $map['username'] = $param['username'];
        }
//        echo'<pre>';print_r($map);exit;
        $list = Db::name('bonus_new')
            ->where($map)
            ->paginate(10,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);

        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 新增
     */
    public function add_bonus(){
        $data = $this->request->param();

        if(empty($data['username'])){
            $this->redirect('ContractIncrease/bonus_new');
        }

        $user_count = Db::name('member')->where('username',$data['username'])->count();

        if(!$user_count){
            $this->error('当前添加的用户不存在');
        }

        $bonus_count = Db::name('bonus_new')->where('username',$data['username'])->count();
        if($bonus_count){
            $this->error('当前用户已添加');
        }

        $bonus = Db::name('bonus_new')->insert($data);
        if($bonus){
            $this->redirect('ContractIncrease/bonus_new');
        }else{
            $this->error('新增失败');
        }
    }

    /**
     * 删除
     * @throws \think\Exception
     */
    public function del(){
        $data = $this->request->param();

        $del = Db::name('bonus_new')->where('username',$data['username'])->delete();

        if($del){
            $this->redirect('ContractIncrease/bonus_new');
        }else{
            $this->error('删除失败');
        }
    }



}